home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr11
/
powerb5.zip
/
P5UTL000.TIP
< prev
next >
Wrap
Text File
|
1993-06-01
|
3KB
|
85 lines
Programs often need to know what key the user just pressed.
If the key in question happens to be a letter, number, or
punctuation symbol, your BASIC programs should have no
trouble recognizing it from its ASCII code. But function and
cursor-control keys don't have ASCII codes. Your program
must recognize them by their keyboard scan codes -- special
codes that are assigned to keystrokes on the IBM PC and
compatibles.
To find out quickly how to recognize any key, I wrote
KEY.BAS. To use it, run KEY.BAS from QuickBASIC or the DOS
5.0 QBASIC, and press the key you want to detect. If the key
can be detected only by its scan code, that's what you'll
see on the left, otherwise, you will see its ASCII code on
the right.
Yaniv Kunda
Ramat-Hasharon, Israel
Editor's Note: The file KEY.BAS appears in the P5UTL
directory of your PowerBase *.* Volume 5 diskette. I've
enhanced Mr. Kunda's program to add an arrow next to the
code for the most recent key typed and to make the screen
display easier to understand. To exit the program at any
time, press <Ctrl>-<Break>.
This tip works because of the QuickBASIC INKEY$ function's
idiosyncrasies. INKEY$ returns an empty string if no key has
been pressed, a string with a single character if an
ordinary key was pressed, or a string with two characters if
a special key (such as a function key) was pressed. If an
ordinary key was pressed, the string contains its ASCII
code. In the case of a special key, the first character of
the string is always a NUL (ASCII code 0), and the second
contains the scan code of the key.
One way to interpret the results of the INKEY$ function in
your own program is shown in the listing below. It uses an
ON...GOTO statement to look repeatedly for keyboard input
and determine what kind of key, if any, was pressed.
When the user presses a character key, the program jumps to
the ORDINARY label; when the user presses a function or
cursor-control key, the program goes to SPECIAL. The code in
LISTING 2 shows what might follow an ORDINARY label where
the user is allowed to press A, B, or C to choose a menu
item, as well as a SPECIAL label where he or she can press
<F1> through <F10>.
---- BEGIN LISTING ----
REM This code fragment shows how to process incoming keystrokes
REM which might include either normal ASCII characters or
REM function keys (but no other special keys).
POLL:
A$ = INKEY$: IF A$ = "" THEN GOTO POLL
IF LEN(A$) > 1 THEN GOTO SPECIAL
ORDINARY:
REM Convert key to a number: 1 for A, 2 for B, etc.
ORDKEY = (ASC(A$) - ASC("A") + 1)
REM Check to see if key is legal
IF ORDKEY <= 0 or ORDKEY > ASC("C") THEN GOTO POLL
REM Go to the routine that processes the key
ON ORDKEY GOTO AKEY, BKEY, CKEY
SPECIAL:
REM Get scan code out of A$.
FKEY = ASC(RIGHT$(A$,1))
REM Deduct 58 from the scan code.
REM This yields 1 for F1, 2 for F2, etc.
REM Reject other special keys
IF FKEY <= 0 OR FKEY > 10 THEN GOTO POLL
REM Go to the routine that processes the key
ON FKEY GOTO F1,F2,F3,F4,F5,F6,F7,F8,F9,F10
---- END LISTING ----
Title: BASIC Key Code Decoder
Category: UTL
Issue Date: February, 1992
Editor: Brett Glass
Supplementary Files: P5UTL\KEY.BAS
Filename: P5UTL000.TIP